<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-form
          :inline="true"
          :model="formInline"
          class="demo-form-inline"
          size="small"
        >
          <el-form-item label="目录名称">
            <el-input v-model="formInline.user" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="formInline.region" placeholder="请选择">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="clear">清除</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="success" icon="el-icon-edit" align="right" @click="showDialog('add')"
              >新增目录</el-button
            >
          </el-form-item>
          <div class="total">
            <span class="el-icon-info"> 数据一共{{ total }}条</span>
          </div>
          <br />
        </el-form>
        <el-table :data="tata" style="width: 100%">
          <el-table-column fixed type="index" label="序号" width="70">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="120">
          </el-table-column>
          <el-table-column prop="directoryName" label="目录名称" width="150">
          </el-table-column>
          <el-table-column prop="username" label="创建者" width="150">
          </el-table-column>
          <el-table-column prop="addDate" label="创建日期" width="240">
            <template #default="{ row }">
              {{ dateFormat(row.addDate) }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="面试题数量" width="150">
          </el-table-column>
          <el-table-column prop="zip" label="状态" width="120">
            <template #default="{ row }">
              <span>{{ row.state === 1 ? "已启用" : "已禁用" }}</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                @click="handleClick(scope.row)"
                type="text"
                size="small"
                v-if="btnStatus == 0"
                >禁用</el-button
              >
              <el-button
                @click="handleClick(scope.row)"
                type="text"
                size="small"
                v-show="btnStatus == 1"
                >启用</el-button
              >
              <el-button type="text" size="small" @click="showDialog(scope.row.id,'edit')" :disabled="submissionFlag"
                >修改</el-button
              >
              <el-button type="text" size="small" @click="deleTe(scope.row.id)" :disabled="submissionFlag"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <template>
          <div class="block">
            <span class="demonstration"></span>
            <el-row
              type="flex"
              justify="end"
              align="middle"
              style="height=: 60"
            >
              <el-pagination
                background
                align="right"
                style="margin-top: 20px"
                :current-page="params.page"
                :page-size="params.pagesize"
                @current-change="handleCurrentChange"
                layout="prev, pager, next, sizes, jumper "
                :page-sizes="[5, 10, 20, 50]"
                :total="total"
                 @size-change="handleSizeChange"
              ></el-pagination>
            </el-row>
          </div>
        </template>
      </el-card>
    </div>
  <edit
  :dialogVisible="dialogVisible"
   @closeDialog="closeDialog"
   :mlId="mlId"
   :type="type"></edit>
  </div>
</template>

<script>
import { list as getCompanys } from '@/api/hmmm/companys'
import { list as getQuestion } from '@/api/hmmm/questions'
import { list, remove } from '../../api/hmmm/directorys.js'
import dayjs from 'dayjs'
import edit from '../components/edit.vue'
export default {
  components: {
    edit
  },
  data () {
    return {
      formInline: {
        user: '',
        region: ''
      },
      submissionFlag: '',
      btnStatus: 0,
      tata: [],
      params: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      dialogVisible: false,
      mlId: 0,
      type: ''
      // currentPage3: 5,
      // currentPage4: 4
    }
  },
  methods: {
    closeDialog () {
      this.dialogVisible = false
    },
    async clear () {
      await list(this.res)

      this.formInline.user = ''
      this.formInline.region = ''
    },
    async onSubmit () {
      // this.getlist()

      const a = this.tata.filter(item => {
      // this.tata.filter(item => {
        return item.directoryName === this.formInline.user
      })

      const res = await list({
        ...this.params,

        directoryName: a[0].directoryName,
        state: this.formInline.region
      })

      this.tata = res.data.items
      this.formInline = {
        user: '',
        region: ''
      }
    },
    dateFormat (str) {
      return dayjs(str).format('YYYY-MM-DD HH:mm:ss')
    },
    handleCurrentChange (newPage) {
      this.params.page = newPage
      this.getlist()
    },
    async getlist () {
      const res = await list(this.params)
      // console.log(res)
      this.tata = res.data.items
      this.total = res.data.counts
    },
    showDialog (id, type) {
      this.mlId = id
      this.dialogVisible = true
      console.log(type)
      this.type = type
    },
    deleTe (id) {
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // //  this.$emit()
        // })
        await remove({ id: id })

        if (this.remove.length === 1 && this.params > 1) {
          this.params.page--
        }
        this.$message.success('删除成功')
        this.getlist()
      })
    },
    handleClick (row) {
      console.log(row)
      this.$message.success('操作成功')
      if (row.state === 1) {
        row.state = 0
        this.btnStatus = 1
        this.submissionFlag = false
      } else if (row.state === 0) {
        row.state = 1
        this.btnStatus = 0
        this.submissionFlag = true
      }
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.params.pagesize = val
      this.getlist()
      // console.log(val)
    }
  },

  // handleCurrentChange (val) {
  //   console.log(`当前页: ${val}`)
  // },

  async created () {
    getCompanys()
    getQuestion()
    this.getlist()
  }
}
</script>

<style scoped>
.total {
  width: 100%;
  height: 35px;
  line-height: 35px;
  padding-left: 15px;
  opacity: 1;
  border-radius: 4px;
  background-color: #f4f4f5;
  color: #909399;
}
span {
  font-size: 13px;
  margin-left: 10px;
  color: gray;
  line-height: 18px;
  opacity: 1;
}
.el-icon-inifo {
  color: gray;
}
</style>
